{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/Qiskit/qiskit-tutorials/master/images/qiskit-heading.png\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"500 px\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Iterative Phase Estimation Algorithm\n",
    "\n",
    "\n",
    "The latest version of this notebook is available on https://github.com/qiskit/qiskit-tutorial.\n",
    "\n",
    "For more information about how to use the IBM Q Experience (QX), consult the [tutorials](https://quantumexperience.ng.bluemix.net/qstage/#/tutorial?sectionId=c59b3710b928891a1420190148a72cce&pageIndex=0), or check out the [community](https://quantumexperience.ng.bluemix.net/qstage/#/community).\n",
    "\n",
    "***\n",
    "### Contributors\n",
    "Antonio Córcoles, Jay Gambetta, Rudy Raymond\n",
    "\n",
    "### Qiskit Package Versions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'qiskit': '0.10.3',\n",
       " 'qiskit-terra': '0.8.1',\n",
       " 'qiskit-ignis': '0.1.1',\n",
       " 'qiskit-aer': '0.2.1',\n",
       " 'qiskit-ibmq-provider': '0.2.2',\n",
       " 'qiskit-aqua': '0.5.1'}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import qiskit\n",
    "qiskit.__qiskit_version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quantum Phase Estimation (QPE)\n",
    "\n",
    "The Quantum Phase Estimation (QPE) algorithm solves the problem of finding unknown eigenvalues of a unitary operator. The attractiveness of the QPE algorithm is due to the fact that it is a key ingredient of some other very powerful algorithms, like order-finding and Shor's. \n",
    "\n",
    "In a standard textbook, such as Nielsen & Chuang <i>Quantum Computation and Quantum Information</i>, in the QPE, each bit of the phase is encoded in a different qubit on a register using the phase kickback property of controlled-unitary operations. This is followed by an inverse Quantum Fourier Transform operation, which yields an n-bit approximation to the phase by reading the n-qubit register."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Iterative Phase Estimation Algorithm (IPEA)\n",
    "The QPE algorithm can, however, be realized in a much smaller qubit system, by iterating the steps on a system of just two qubits. This is called the Iterative Phase Estimation Algorithm (IPEA).\n",
    "\n",
    "Consider the problem of finding $\\varphi$ given $|\\Psi\\rangle$ and $U$ in $U |\\Psi\\rangle = e^{i \\phi} | \\Psi \\rangle$, with $\\phi = 2 \\pi \\varphi$. Let's assume for now that $\\varphi$ can be written as $\\varphi = \\varphi_1/2 + \\varphi_2/4 + ... + \\varphi_m/2^m = 0.\\varphi_1 \\varphi_2 ... \\varphi_m$, where we have defined the notation $0.\\varphi_1 \\varphi_2 ... \\varphi_m$. Now, if we have two qubits, $q_0$ and $q_1$, and we initialize them as $q_0 \\rightarrow |+\\rangle$ and $q_1 \\rightarrow |\\Psi \\rangle$, then, after applying a control-U between $q_0$ and $q_1$ $2^t$ times, the state of $q_0$ can be written as $|0\\rangle + e^{i 2 \\pi 2^{t} \\varphi} | 1 \\rangle$. That is, the phase of $U$ has been kicked back into $q_0$ as many times as the control operation has been performed.\n",
    "\n",
    "For $t=0$, we have a total phase in $q_0$ of $e^{i 2 \\pi 2^{0} \\varphi} = e^{i 2 \\pi \\varphi} = e^{i 2 \\pi 0.\\varphi_1 \\varphi_2 ... \\varphi_m}$\n",
    "\n",
    "For $t=1$, the phase would be $e^{i 2 \\pi 2^{1} \\varphi} = e^{i 2 \\pi \\varphi_1} e^{i 2 \\pi 0.\\varphi_2 \\varphi_3 ... \\varphi_m}$\n",
    "\n",
    "For $t=2$, $e^{i 2 \\pi 2^{2} \\varphi} = e^{i 2 \\pi 2 \\varphi_1} e^{i 2 \\pi \\varphi_2} e^{i 2 \\pi 0.\\varphi_3 \\varphi_4 ... \\varphi_m}$\n",
    "\n",
    "And for $t=m-1$, $e^{i 2 \\pi 2^{m-1} \\varphi} = e^{i 2 \\pi 2^{m-2} \\varphi_1} e^{i 2 \\pi 2^{m-3} \\varphi_2} ... e^{i 2 \\pi 2^{-1} \\varphi_m} = e^{i 2 \\pi 0.\\varphi_m}$. Note that if we perform a Hadamard operation on the state $|0\\rangle + e^{i 2 \\pi 0.\\varphi_m}|1\\rangle$ and perform a measurement in the standard basis, we obtain $|0\\rangle$ if $\\varphi_m = 0$ and $|1\\rangle$ if $\\varphi_m = 1$. \n",
    "\n",
    "In the first step of the IPEA, we directly measure the least significant bit of the phase $\\varphi$, $\\varphi_m$, by initializing the 2-qubit register as described above, performing $2^{m-1}$ control-$U$ operations between the qubits, and measuring $q_0$ in the diagonal basis.\n",
    "\n",
    "For the second step, we initialize the register in the same way and apply $2^{m-2}$ control-$U$ operations. The phase in $q_0$ after these operations is now $e^{i 2 \\pi 0.\\varphi_{m-1}\\varphi_{m}}= e^{i 2 \\pi 0.\\varphi_{m-1}} e^{i 2 \\pi \\varphi_m/4}$. We see that prior to extracting the phase bit $\\varphi_{m-1}$, we must perform a phase correction of $\\varphi_m /2$. This is equivalent to a rotation around the $Z-$axis of angle $-\\varphi_m /4$.\n",
    "\n",
    "Therefore, the $k$th step of the IPEA, giving $\\varphi_{m-k+1}$, consists of the register initialization ($q_0$ in $|+\\rangle$, $q_1$ in $|\\Psi\\rangle$), the application of control-$U$ $2^{m-k}$ times, a rotation around $Z$ of angle $\\omega_k = -2 \\pi 0.0\\varphi_{k+1} ... \\varphi_m$, a Hadamard transform to $q_0$, and a measurement of $q_0$ in the standard basis. Note that $q_1$ remains in the state $|\\Psi\\rangle$ throughout the algorithm."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## IPEA circuit\n",
    "\n",
    "Let's first initialize the API and import the necessary packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:28:50.747843Z",
     "start_time": "2018-09-26T14:28:48.229088Z"
    }
   },
   "outputs": [],
   "source": [
    "from math import pi\n",
    "import numpy as np\n",
    "import scipy as sp\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "# importing Qiskit\n",
    "from qiskit import BasicAer, IBMQ\n",
    "from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister\n",
    "from qiskit import execute\n",
    "from qiskit.tools.visualization import plot_histogram\n",
    "from qiskit.tools.monitor import job_monitor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:28:51.950686Z",
     "start_time": "2018-09-26T14:28:50.750412Z"
    }
   },
   "outputs": [],
   "source": [
    "# Load saved IBMQ accounts\n",
    "IBMQ.load_accounts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now you can try the following circuit in the quantum simulator for a phase of $-5\\pi/8 = 2 \\pi \\varphi$ and $m=4$. Note that the IPEA cannot be run in the real device in this form, due to the current lack of feedback capability."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:28:53.401913Z",
     "start_time": "2018-09-26T14:28:51.952881Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAc0AAAFLCAYAAAC0rNfYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAbdElEQVR4nO3de7RdZXnv8e/DRkpyhJCEfZLs2Gi2HnGLuAkJbQm3cIlV9HgQHICXcvHCQQRURA0d1oqtOBoVoSpF0A4QKVDwUquhQEgKFBBICBqMRoOQHEgCkgSlgZgQnvPHWsGVlX15V9iXlazvZ4w19prvfOe7nvnP/o15e2dkJpIkqX+7DHcBkiTtKAxNSZIKG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+UnIVMB44LyLGVcOydpL2XwGvAp4ahhKlHYKnZ6UWUL1rdhfgNOBCKvczfBe4HlgBvBF4G/D6zDxwuOqUmp2hKbWY6jsyTwXeDewPPEPlDtr7gS9k5r3DV53U3AxNaSdXnTbvmbqXS+8C7A68HHgDsN6wlPpnaEo7uYj4BpXJ2e8Dlmfm73voMzoz19W+nFrStgxNaScWEe8CrgF+D6wFbqXyNpOfAY9n5nMR8XLgO8DfZObiYStW2gEYmtJOLCKuADYDs4HjqLwj89XAUmAOcBuVCQ8uyczdhqtOaUdhaEo7qeqzmZ8E9szMWTXt+wIfBN5J5brmXsBVmfn+YSlU2oEYmtJOLCJGA+My85cRsRuwqe6GoBOBa4EDMvPB4apT2lE496y0E8vMdcC66veN8OKds5GZm4E9gQ0GplTG0JRaTN0sQHsAfztctUg7Gk/PSi0sIl4G1E+nJ6kXhqYkSYWcsF2SpEKGpiRJhQxNSZIKGZqSJBUyNCVJKmRoSpJU6P8DwNiGguStNi8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# We first define controlled gates used in the IPEA\n",
    "def cu1fixed(qProg, c, t, a):\n",
    "    qProg.u1(-a, t)\n",
    "    qProg.cx(c, t)\n",
    "    qProg.u1(a, t)\n",
    "    qProg.cx(c, t)\n",
    "\n",
    "def cu5pi8(qProg, c, t):\n",
    "    cu1fixed(qProg, c, t, -5.0*pi/8.0)\n",
    "\n",
    "# We then prepare quantum and classical registers and the circuit\n",
    "qr = QuantumRegister(2)\n",
    "cr = ClassicalRegister(4)\n",
    "circuitName=\"IPEAonSimulator\"\n",
    "ipeaCircuit = QuantumCircuit(qr, cr)\n",
    "\n",
    "# Apply IPEA\n",
    "ipeaCircuit.h(qr[0])\n",
    "for i in range(8):\n",
    "    cu5pi8(ipeaCircuit, qr[0], qr[1])\n",
    "ipeaCircuit.h(qr[0])\n",
    "ipeaCircuit.measure(qr[0], cr[0])\n",
    "\n",
    "ipeaCircuit.reset(qr[0])\n",
    "\n",
    "ipeaCircuit.h(qr[0])\n",
    "for i in range(4):\n",
    "    cu5pi8(ipeaCircuit, qr[0], qr[1])\n",
    "ipeaCircuit.u1(-pi/2, qr[0]).c_if(cr, 1)\n",
    "ipeaCircuit.h(qr[0])\n",
    "ipeaCircuit.measure(qr[0], cr[1])\n",
    "\n",
    "ipeaCircuit.reset(qr[0])\n",
    "\n",
    "ipeaCircuit.h(qr[0])\n",
    "for i in range(2):\n",
    "    cu5pi8(ipeaCircuit, qr[0], qr[1])\n",
    "ipeaCircuit.u1(-pi/4, qr[0]).c_if(cr, 1)\n",
    "ipeaCircuit.u1(-pi/2, qr[0]).c_if(cr, 2)\n",
    "ipeaCircuit.u1(-3*pi/4, qr[0]).c_if(cr, 3)\n",
    "ipeaCircuit.h(qr[0])\n",
    "ipeaCircuit.measure(qr[0], cr[2])\n",
    "\n",
    "ipeaCircuit.reset(qr[0])\n",
    "\n",
    "ipeaCircuit.h(qr[0])\n",
    "cu5pi8(ipeaCircuit, qr[0], qr[1])\n",
    "ipeaCircuit.u1(-pi/8, qr[0]).c_if(cr, 1)\n",
    "ipeaCircuit.u1(-2*pi/8, qr[0]).c_if(cr, 2)\n",
    "ipeaCircuit.u1(-3*pi/8, qr[0]).c_if(cr, 3)\n",
    "ipeaCircuit.u1(-4*pi/8, qr[0]).c_if(cr, 4)\n",
    "ipeaCircuit.u1(-5*pi/8, qr[0]).c_if(cr, 5)\n",
    "ipeaCircuit.u1(-6*pi/8, qr[0]).c_if(cr, 6)\n",
    "ipeaCircuit.u1(-7*pi/8, qr[0]).c_if(cr, 7)\n",
    "ipeaCircuit.h(qr[0])\n",
    "ipeaCircuit.measure(qr[0], cr[3])\n",
    "\n",
    "backend = BasicAer.get_backend('qasm_simulator')\n",
    "shots = 1000\n",
    "\n",
    "results = execute(ipeaCircuit, backend=backend, shots=shots).result()\n",
    "plot_histogram(results.get_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The results are given in terms of $\\varphi = 0.\\varphi_1 \\varphi_2 \\varphi_3 \\varphi_4$, with the least significant digit ($\\varphi_4$) as the leftmost bit in the classical register. The result is $\\varphi = 11/16$, from which $\\phi = 2\\pi \\varphi = 11 \\pi/8 = 2 \\pi - 5\\pi/8$, as encoded in the circuit. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## IPEA in the real device\n",
    "\n",
    "As we have mentioned before, we currently lack the ability to use measurement feedback or feedforward, along with qubit resetting, on the real device in the Quantum Experience. However, we still can implement a segmentized version of the IPEA by extracting the information about the phase one bit at a time.\n",
    "\n",
    "Try the following four circuits in the real device. They estimate the same phase as in the previous example (-5$\\pi/8$), one bit at a time, from least ($\\varphi_4$) to most ($\\varphi_1$) significant bit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:28:55.118404Z",
     "start_time": "2018-09-26T14:28:53.403783Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Available backends: [[<QasmSimulatorPy('qasm_simulator') from BasicAer()>, <StatevectorSimulatorPy('statevector_simulator') from BasicAer()>, <UnitarySimulatorPy('unitary_simulator') from BasicAer()>], [<IBMQBackend('ibmqx4') from IBMQ()>, <IBMQBackend('ibmqx2') from IBMQ()>, <IBMQBackend('ibmq_16_melbourne') from IBMQ()>, <IBMQSimulator('ibmq_qasm_simulator') from IBMQ()>]]\n",
      "Job Status: job has successfully run\n"
     ]
    }
   ],
   "source": [
    "# We then prepare quantum and classical registers and the circuit\n",
    "qr = QuantumRegister(5)\n",
    "cr = ClassicalRegister(5)\n",
    "realStep1Circuit = QuantumCircuit(qr, cr)\n",
    "\n",
    "# Apply IPEA\n",
    "realStep1Circuit.h(qr[0])\n",
    "for i in range(8):\n",
    "    cu5pi8(realStep1Circuit, qr[0], qr[1])\n",
    "realStep1Circuit.h(qr[0])\n",
    "realStep1Circuit.measure(qr[0], cr[0])\n",
    "\n",
    "#connect to remote API to be able to use remote simulators and real devices\n",
    "print(\"Available backends:\", [BasicAer.backends(), IBMQ.backends()])\n",
    "\n",
    "backend = IBMQ.get_backend(\"ibmq_5_yorktown\")\n",
    "shots = 1000\n",
    "   \n",
    "job_exp1 = execute(realStep1Circuit, backend=backend, shots=shots)\n",
    "job_monitor(job_exp1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:46:51.283780Z",
     "start_time": "2018-09-26T14:28:55.120518Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAcQAAAFTCAYAAACnPHzkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3dfZzWdZ3v8ddH7kIEYZDFGREQsXG4WXbETNLUTS207WbJo21mt+aqm3v2uJtpx03ttHbW0nTbPLVW3rXnbGW0rRnF8eB6g6YGxIJOBIzAIiMhELKADEyf88fvGnacBpjBmeuCmdfz8ZgH1/W7mflcjF/f/L6/7/f7i8xEkqS+7rBKFyBJ0sHAQJQkCQNRkiTAQJQkCTAQJUkCDERJkgDoX+kCesrIkSNz7NixlS5DknQQ+cUvfvFyZo7qaF+vDcSxY8cyb968SpchSTqIVFVVrd7bPrtMJUnCQJQkCTAQJUkCKhCIEXFlRLwQEa9GxIKIeOt+jv9ARPwiIrZHxEsR8e2IOLpc9UqS+oayBmJEXATcAdwM1ANPAnMiosPhoBFxGnA/cC8wGXgvMAn4x7IULEnqM8p9hXg1cE9m3pWZDZl5FdAEXLGX42cAazPzy5n5Qmb+DPgK8OYy1StJ6iPKFogRMRC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zEvuEB4LfAX0TE0MzcmZnNEXEYQGauAX4DjKlkkZL2sM1WgIHYy5XuL2wCrqcYrr0uIr4ZEdNL+8dGxAcp5jd9t3KVSgLbbCV5D7GPiIjhwFiKZ6f9MXBaaddLQAD3Z+aNlalOUnu22fIzEHuxiPg94BLgL4GXKeYv/QZ4AvgZMIBijtNPMvNXlapTUsE2W1kGYi8WEfcAk4EHKbpgqii6Wd4I/Bq4PjOfrliBkl7DNltZBmIvVboPsRU4PzMfa7NtLPBm4FKK1S4uzMyFFStUEmCbPRg4qKb3mgS8QDF8G4AsrM7M7wLvouiK+S8Vqk/Sa9lmK8xA7L0aKbpYvhwRJ7QO126VmTuBeymeuC2p8myzFWYg9lKZuQP478Bg4D7gQxFxbEQcARARhwNnAksrV6WkVrbZyvMeYi9XWunir4F3U6yW/xSwATgHaAIuzcwllatQUlu22coxEPuI0nDudwLvpVjyaSnwvcz8ZUULk9Qh22z5GYh9UEQclpm/rXQdkjrHNlseBqIkSTioRpIkwECUJAkwECVJAgxESZIAA1GSJMBAlCQJMBAlSQLg/wOLr+gMkjg/vAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results1 = job_exp1.result()\n",
    "plot_histogram(results1.get_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the first step of IPEA as above, we obtain the bit \"1\" with probability close to one. We then proceed to the second step of IPEA, assuming that we have identified the result of the first step correctly, as below. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:46:51.501883Z",
     "start_time": "2018-09-26T14:46:51.285861Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r",
      "Job Status: job has successfully run\n"
     ]
    }
   ],
   "source": [
    "realStep2Circuit = QuantumCircuit(qr, cr)\n",
    "\n",
    "# Apply IPEA\n",
    "realStep2Circuit.h(qr[0])\n",
    "for i in range(4):\n",
    "    cu5pi8(realStep2Circuit, qr[0], qr[1])\n",
    "realStep2Circuit.u1(-pi/2, qr[0]) # Assuming the value of the measurement on Step 1\n",
    "realStep2Circuit.h(qr[0])\n",
    "realStep2Circuit.measure(qr[0], cr[0])\n",
    "\n",
    "job_exp2 = execute(realStep2Circuit, backend=backend, shots=shots)\n",
    "job_monitor(job_exp1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:47:15.561184Z",
     "start_time": "2018-09-26T14:46:51.503838Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAcQAAAFTCAYAAACnPHzkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3dfZzWdZ3v8ddH7kJCZZCFQRqQ0NkBWc4IuUmlpNJBuzlGHN2yolrXVcu2Y63dHOtY7VKZmq5lmlt5t+csbeJ2S5nSRqauBUSCSLojsMhIOBCQIOD4OX/8LmgYZ2Aud+a6ZOb1fDzmwfW7+V7zubz4+uZ38/3+IjORJKmvO6zaBUiS9FJgIEqShIEoSRJgIEqSBBiIkiQBBqIkSQD0r3YBPWX48OFZV1dX7TIkSS8hv/71r5/OzBEdbeu1gVhXV8eiRYuqXYYk6SWkpqZmbWfbPGUqSRIGoiRJgIEoSRJgIEqSBBiIkiQBBqIkSYCBKEkSYCBKkgQYiJIkAQaiJEmAgShJEmAgSpIEGIiSJAEGoiRJgIEoSRJgIEqSBBiIkiQBBqIkSYCBKEkSYCBKkgQYiJIkAQaiJEmAgShJLwn33HMPJ510ElOnTuXaa699wfb169fzlre8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xL5hNzA0Ik6AYl7EiBgIkJn3AedRPGT0DdUrUVIb9tkqMBD7hm8DzwMfioihmbkrM3dHxGEAmbkO+D0wpppFStrHPlsFBmIvV7q+sBm4nOJ27Q0R8fWImFraXhcR76QY3/St6lUqCeyz1eQ1xD4iIo4C6iienfZW4DWlTU8BAdyemVdUpzpJ7dlnK89A7MUi4k+AdwEfBp6mGL/0e+A+4EFgAMUYpx9l5m+rVaekgn22ugzEXiwibgEmAd+jOAVTQ3Ga5Xjgd8DlmfnvVStQ0n7ss9VlIPZSpesQ24GzMnNxm3V1wJ8D51PMdnFOZi6tWqGSAPvsS4E31fReE4EnKG7fBiALazPzW8CbKU7F/M8q1Sdpf/bZKjMQe68milMsX4qI4/berr1XZu4CbqV44rak6rPPVpmB2Etl5k7gfwODgduAd0fEKyLi5QARcThwKrCielVK2ss+W31eQ+zlSjNdfBJ4C8Vs+Q8Am4AzgGbg/Mx8uHoVSmrLPls9BmIfUbqd+43A2RRTPq0A/iUzH61qYZI6ZJ+tPAOxD4qIwzLz+WrXIalr7LOVYSBKkoQ31UiSBBiIkiQBBqIkSYCBKEkSYCBKkgQYiJIkAQaiJEkA/H9wuBmF5kPThwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results2 = job_exp2.result()\n",
    "plot_histogram(results2.get_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the second step of IPEA as above, we obtain the bit \"1\" with probability close to one. We then proceed to the third step of IPEA, assuming that we have identified the result of the first and second steps correctly, as below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:47:15.760111Z",
     "start_time": "2018-09-26T14:47:15.563852Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Job Status: job has successfully run\n"
     ]
    }
   ],
   "source": [
    "realStep3Circuit = QuantumCircuit(qr, cr)\n",
    "\n",
    "# Apply IPEA\n",
    "realStep3Circuit.h(qr[0])\n",
    "for i in range(2):\n",
    "    cu5pi8(realStep3Circuit, qr[0], qr[1])\n",
    "realStep3Circuit.u1(-3*pi/4, qr[0]) # Assuming the value of the measurement on Step 1 and Step 2\n",
    "realStep3Circuit.h(qr[0])\n",
    "realStep3Circuit.measure(qr[0], cr[0])\n",
    "\n",
    "job_exp3 = execute(realStep3Circuit, backend=backend, shots=shots)\n",
    "job_monitor(job_exp3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:47:40.065810Z",
     "start_time": "2018-09-26T14:47:15.761971Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAc0AAAFTCAYAAABbKVcuAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3df5xV9X3n8ddHfokKyBAWZyQjIgRHsDjK2miiosbWmCY16GpSrTHZ1DW/Wusm5keTbE02dEMTa7YmNTXNqkk3q0aibRoaaiBqxMQVkAJOKGQEFxkJKiIRZAA/+8e50GEc4AzO3Dswr+fjcR9z7znfc+ZzuI8v7zm/vicyE0mStH+H1boASZIOFoamJEklGZqSJJVkaEqSVJKhKUlSSYamJEklDax1AbU0atSobGxsrHUZkqQ+5IknnnguM0d3Na9fh2ZjYyPz5s2rdRmSpD6krq5uzd7meXhWkqSSDE1JkkoyNCVJKsnQlCSpJENTkqSSDE1JkkoyNCVJKsnQlCSpJENTkqSSDE1JkkoyNCVJKsnQlCSppKqGZkScHRH/EBHPRERGxNUlljk5Ih6MiK2V5T4fEdGpzSUR8WREbKv8fHevb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7Mfy8ytwJ8BQ4E7gasi4o0RcRRARBwBnAMsq12Vknaxz9ae5zRFZUSRzwHvonhKwqPABuBtQBvwwcxcWrsKJXVkn60dQ1O7VS5lfwdwMcXwW8uAezLzlzUtTFKX7LPVZ2iqSxFxWGa+Wus6JJVjn60OQ1OSpJK8EEiSpJIMTUmSSjI0JUkqydCUJKkkQ1OSpJIMTUmSSjI0JUkq6f8DekuDZa8AaiAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results3 = job_exp3.result()\n",
    "plot_histogram(results3.get_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the third step of IPEA as above, we obtain the bit \"0\" with probability close to one. We then proceed to the fourth step of IPEA, assuming that we have identified the result of the first, second, and third steps correctly, as below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:47:40.199847Z",
     "start_time": "2018-09-26T14:47:40.069259Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Job Status: job has successfully run\n"
     ]
    }
   ],
   "source": [
    "realStep4Circuit = QuantumCircuit(qr, cr)\n",
    "\n",
    "# Apply IPEA\n",
    "realStep4Circuit.h(qr[0])\n",
    "cu5pi8(realStep4Circuit, qr[0], qr[1])\n",
    "realStep4Circuit.u1(-3*pi/8, qr[0]) # Assuming the value of the measurement on Step 1, 2, and 3\n",
    "realStep4Circuit.h(qr[0])\n",
    "realStep4Circuit.measure(qr[0], cr[0])\n",
    " \n",
    "job_exp4 = execute(realStep4Circuit, backend=backend, shots=shots)\n",
    "job_monitor(job_exp4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-26T14:48:04.209177Z",
     "start_time": "2018-09-26T14:47:40.202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5sdLnvwI/B1YAo4A/pAjNq6q0TpKkAaKqoZmZ90TEGOBzFPdTLgYuzMxVlS5d79e8kqLGWyqvDo8C51Tevxb4NnAssBloAc7KzCf7Yh0kSQNXtfc0ycxbgVv3Me2c/X3exzx/DPxxb9QmSdL+OPasJEklGZqSJJVkaEqSVJKhKUlSSYamJEklGZqSJJVkaEqSVJKhKUlSSYamJEklGZqSJJVkaEqSVJKhKUlSSYamJEklGZqSJJVkaEqSVJKhKUlSSYamJEklGZoD3MMPP8z06dM5/fTTueWWW14xfd68eZxzzjmMHTuW+++/f69pM2fOZOLEibzvfe/bq/2KK65g+vTpnHnmmVx99dXs3r27T9dBkqrF0BzA2tvbuf7665k9ezaPP/449957L08//fRefcaPH89f/uVfMnPmzFfMf80113Dbbbe9ov13f/d3eeKJJ3jsscfYsWMHd999d5+tgyRVk6E5gM2fP5/jjz+eiRMnMnToUC655BIefPDBvfo0NjYydepUjjjilf+pnH322Rx11FGvaD///POJCCKC0047jbVr1/bZOkhSNRmaA1hbWxvHHXfcns8NDQ20tbX12vJ3797N7NmzOe+883ptmZJUS4am+synPvUpZsyYwYwZM2pdiiT1isG1LkC1U19fz3PPPbfn89q1a6mvr++VZd94441s2LCBm2++uVeWJ0n9gXuaA9hpp51Ga2srq1atYteuXdx3331ccMEFr3q5d911F3PmzOH222/v9lyoJB2q/D/aADZ48GBmzZrFzJkzedOb3sTFF19MU1MTN9xww54LghYsWMDUqVO5//77ue666/Y61HrhhRfykY98hLlz5zJ16lR+/OMfA/DJT36S9evX89u//ducddZZzJo1qybrJ0m9LTKz1jXUTHNzc86ZM6fWZUiqsuvvHF3rEtQHZl2+qVeWU1dXNz8zz+humnuakiSVZGhKklSSV8/2Ag/1HJ5661CPpMOHe5qSJJVkaEqSVJKhKUlSSYamJEklGZqSJJVkaEqSVJKhKUlSSYamJEklGZqSJJVkaEqSVJKhKUlSSYamJEklGZqSJJVkaEqSVJKhKUlSSYamJEklGZqSJJVkaEqSVJKhKUlSSYamJEkl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NvAv4W2AV8DPiflXvB8F4vqfbcZmvDPU0BEBHXAB/MzDd2M+0twFeA44DpmflCteuTtDe32dpwT1MddgEjI+JkKMaxjIihAJn5M+AyigfZ/lbtSpTUidtsDRia6vB94GXg2ogYmZk7M3NXRBwBkJmrgV8D42tZpKQ93GZrwNBUx/mOjcDnKC5VXxsRfxMRp1emN0bEBynu/5pdu0olgdtsLXlOU3tExGuBRopn770beHNl0vNAAHdn5l/UpjpJXbnNVp+hOcBFxH8Cfg/4JPACxf1dvwZ+BvwcGEJxD9g/Z+a/16pOSQW32doyNAe4iLgDmAr8I8XhnjqKQzonAr8CPpeZT9SsQEl7cZutLUNzAKucF9kCXJiZczu1NQJvBD5KMarIezNzQc0KlQS4zfYHXgg0sJ0EPEtx6ToAWViVmbOB36E47PO7NapP0t7cZmvM0BzYWikO59wcESd0XKreITN3AndSPPldUu25zdaYoTmAZeZ24M+A4cBdwIci4nURcRRARIwAzgYW165KSR3cZmvPc5qiMqLInwMXUTwl4XFgPfB2oA34aGYuql2Fkjpzm60dQ1N7VC5lfydwMcXwW4uB72Xm0zUtTFK33Garz9BUtyLiiMx8udZ1SCrHbbY6DE1JkkryQiBJkkoyNCVJKsnQlCSpJENTkqSSDE1JkkoyNCVJKsnQlCSppP8Hb786cHnPHFIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results4 = job_exp4.result()\n",
    "plot_histogram(results4.get_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the fourth step of the IPEA, we identify the bit \"1\" with high probability. In summary, we can conclude with high probability that the binary string of the phase is \"1011\"; that is, eleven in the decimal.  \n",
    "\n",
    "We have left aside the case when $\\varphi$ does not accept a decomposition of the form $\\varphi = \\varphi_1/2 + \\varphi_2/4 + ... + \\varphi_m/2^m$. In that case, it can be shown that we can still use the IPEA to obtain $\\varphi$ to an accuracy of $2^{-m}$ with greater than a constant probability independent of $m$ (around $81\\%$ [1])."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### References\n",
    "\n",
    "[1] M. Dobsicek *et al. Phys. Rev. A* **76**, 030306 (2007)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "latex_envs": {
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 0
  },
  "nav_menu": {},
  "toc": {
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 6,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
